<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="$t('创建采购单')"
    width="1500px"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <div class="my-dialog">
      <el-form class="left-form" ref="elForm" :model="form" :rules="rules" size="small" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 采购类型 1-国内采购 2-酒水采购 3-日本当地采购 -->
            <el-form-item :label="$t('采购类型')" prop="type">
              <el-select @change="setVisible" style="width: 100%" v-model="form.type">
                <el-option
                  :label="item.label"
                  :value="+item.value"
                  v-for="(item, index) in purchase_type_list"
                  :key="index"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('汇率')">
              <b>{{ exchangeRate }}</b>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 除了酒水 -->
        <div v-if="form.type != 2">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="sku">
                {{ form.skuNo }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('采购数量')" prop="num">
                <el-input-number
                  @change="setSkuUnitPrice"
                  :precision="0"
                  style="width: 100%"
                  v-model="form.num"
                  controls-position="right"
                  :min="1"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="40">
            <el-col :span="12" style="padding-right: 10px">
              <el-form-item :label="$t('付款总额')" prop="payPrice">
                <el-input
                  type="number"
                  @change="setInfo"
                  style="width: 100%"
                  :min="0"
                  v-model="form.payPrice"
                  class="input-with-select"
                >
                  <!-- 付款总价单位 1-RMB 2-JPY 3-USD -->
                  <el-button slot="append">
                    <el-select @change="handleExchangeRate" style="width: 100px" v-model="form.payPriceUnit">
                      <el-option label="RMB" :value="1" />
                      <el-option label="JPY" :value="2" />
                      <el-option label="USD" :value="3" />
                    </el-select>
                  </el-button>
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :span="6" style="padding-right: 0px">
              <el-form-item :label="$t('总货值')" label-width="70px" prop="skuTotalValue">
                <el-input disabled style="width: 100%" v-model="form.skuTotalValue">
                  <template slot="suffix">JPY</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="50px" :label="$t('单价')" prop="skuUnitPrice">
                <el-input disabled style="width: 100%" v-model="form.skuUnitPrice">
                  <template slot="suffix">JPY</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="40">
            <el-col :span="12" style="padding-right: 10px">
              <el-form-item label-width="120px" :label="$t('该货币的单价')">
                <el-input disabled style="width: 100%" v-model="unitPrice">
                  <!-- 付款总价单位 1-RMB 2-JPY 3-USD -->
                  <el-button slot="append">
                    <el-select disabled style="width: 100px" v-model="form.payPriceUnit">
                      <el-option label="RMB" :value="1" />
                      <el-option label="JPY" :value="2" />
                      <el-option label="USD" :value="3" />
                    </el-select>
                  </el-button>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 厂家 -->
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-radio-group @change="handleRadioChange" v-model="form.choice">
                  <el-radio :label="3">{{ $t('选择已采购厂家') }}</el-radio>
                  <el-radio :label="1">{{ $t('选择所有厂家') }}</el-radio>
                  <el-radio :label="2" v-if="form.type != 2">{{ $t('新增厂家') }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 已采购厂家 -->
          <el-row v-if="form.choice == 3">
            <el-col :span="24">
              <el-form-item :label="$t('已采购厂家')" prop="manufacturersId">
                <el-select
                  filterable
                  @change="
                    (e) => {
                      handleManufacturerDetail(e, 'old')
                    }
                  "
                  style="width: 100%"
                  v-model="form.manufacturersId"
                >
                  <el-option
                    v-for="(item, index) in manufacturersListOld"
                    :key="index"
                    :label="item.manufacturersName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 厂家  选择所有厂家-->
          <el-row v-if="form.choice == 1">
            <el-col :span="24">
              <el-form-item :label="$t('厂家')" prop="manufacturersId">
                <el-select
                  filterable
                  @change="
                    (e) => {
                      handleManufacturerDetail(e)
                    }
                  "
                  style="width: 100%"
                  v-model="form.manufacturersId"
                >
                  <el-option
                    v-for="(item, index) in manufacturersList"
                    :key="index"
                    :label="item.manufacturersName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 厂家详情 -->
          <div style="padding: 0px 0px 20px 30px" v-if="form.choice == 1 || form.choice == 3">
            <el-descriptions :column="2" border>
              <el-descriptions-item :label="$t('厂家名称')">
                {{ manufacturerDetail.manufacturersName }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('')">
                {{ manufacturerDetail.manufacturersTel }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('厂家地址')">
                {{ manufacturerDetail.manufacturersAddress }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('负责人名称')">
                {{ manufacturerDetail.principalName }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('负责人电话')">
                {{ manufacturerDetail.principalTel }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('网络联系方式(qq/微信/邮箱)')">
                {{ manufacturerDetail.netConcat }}
              </el-descriptions-item>
              <el-descriptions-item :label="$t('厂家链接')">
                <el-link :href="manufacturerDetail.purchasePlatformLink" target="_blank" type="success">{{
                  manufacturerDetail.purchasePlatformLink
                }}</el-link>
              </el-descriptions-item>
              <el-descriptions-item :label="$t('产品链接')">
                <el-link :href="manufacturerDetail.skuUrl" target="_blank" type="success">
                  {{ manufacturerDetail.skuUrl }}
                </el-link>
                <el-input placeholder="https://www.amazon.co.jp/dp/B0048CF0PG" v-model="manufacturerDetail.skuUrl">
                  <div slot="append">
                    <el-button
                      :disabled="!manufacturerDetail.manufacturersName"
                      @click="handleUpdateManufacturersSkuUrl"
                      icon="el-icon-edit"
                      v-hasPermi="['distribution:manufacturersSku:updateManufacturersSkuUrl']"
                    >
                      {{ $t('修改') }}
                    </el-button>
                  </div>
                </el-input>
              </el-descriptions-item>
            </el-descriptions>
          </div>
          <!-- 新增厂家 -->
          <div
            v-if="form.choice == 2"
            style="padding: 10px 80px; border: 1px dashed #c9c9c9; border-radius: 8px; margin-bottom: 20px"
          >
            <el-form ref="addForm" :model="addForm" :rules="addRules" label-position="top">
              <el-row :gutter="50">
                <el-col :span="12">
                  <el-form-item :label="$t('厂家名称')" prop="manufacturersName">
                    <el-input v-model="addForm.manufacturersName" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('厂家电话')" prop="manufacturersTel">
                    <el-input v-model="addForm.manufacturersTel" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="50">
                <el-col :span="12">
                  <el-form-item :label="$t('厂家地址')" prop="manufacturersAddress">
                    <el-input v-model="addForm.manufacturersAddress" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('负责人名称')" prop="principalName">
                    <el-input v-model="addForm.principalName" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="50">
                <el-col :span="12">
                  <el-form-item :label="$t('负责人电话')" prop="principalTel">
                    <el-input v-model="addForm.principalTel" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('网络联系方式(qq/微信/邮箱)')" prop="netConcat">
                    <el-input v-model="addForm.netConcat" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="50">
                <el-col :span="12">
                  <el-form-item :label="$t('厂家链接')" prop="purchasePlatformLink">
                    <el-input v-model="addForm.purchasePlatformLink" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('产品链接')" prop="skuUrl">
                    <el-input v-model="addForm.skuUrl" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
        <div v-if="form.type == 2" style="margin: 20px 0">
          <div>
            <el-button>{{ $t('下载模板') }}</el-button>
            <el-button>{{ $t('导入模板') }}</el-button>
          </div>
        </div>

        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('备注')" prop="remark">
              <el-input v-model="form.remark" :rows="4" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <div style="text-align: right">
            <el-button @click="showDialog = false">{{ $t('取消') }}</el-button>
            <el-button type="primary" @click="submitForm">{{ $t('确定') }}</el-button>
          </div>
        </el-form-item>
      </el-form>

      <div class="right-table">
        <!-- 最近采购记录 -->
        <div style="flex: 1">
          <div class="right-table_title">{{ $t('最近采购记录') }}</div>
          <el-table :height="calTableHeight() / 2" class="table" :data="skuPurchaseRecord.tableData">
            <!-- sku -->
            <el-table-column label="sku" prop="skuNo" width="120" align="center" />
            <el-table-column :label="$t('采购数量')" prop="num" align="center" />
            <el-table-column show-overflow-tooltip :label="$t('采购总价/币种')" align="center">
              <template slot-scope="{ row }">
                <div>{{ row.payPrice }}</div>
                <div :style="handlePayPriceUnit(row.payPriceUnitStr)">{{ row.payPriceUnitStr }}</div>
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip :label="$t('采购单价/币种')" align="center">
              <template slot-scope="{ row }">
                <div>{{ ((row.payPrice || 0) / (row.num || 0)).toFixed(2) }}</div>
                <div :style="handlePayPriceUnit(row.payPriceUnitStr)">{{ row.payPriceUnitStr }}</div>
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              :label="$t('创建时间')"
              prop="createTime"
              align="center"
              width="170"
            />
          </el-table>
          <pagination
            :total="skuPurchaseRecord.pageTotal"
            :page.sync="skuPurchaseRecord.queryParams.pageNum"
            :limit.sync="skuPurchaseRecord.queryParams.pageSize"
            @pagination="getSkuPurchaseList"
          />
        </div>
        <!-- 厂家关联sku -->
        <div style="flex: 1">
          <div class="right-table_title">{{ $t('厂家关联sku') }}</div>
          <el-table :height="calTableHeight() / 2" class="table" :data="manufacturersSku.tableData">
            <!-- sku -->
            <el-table-column label="sku" prop="skuNo" width="120" align="center" />

            <!-- 图片 -->
            <el-table-column :label="$t('图片')" align="center">
              <template slot-scope="{ row }">
                <el-image
                  style="width: 40px; height: 40px"
                  fit="contain"
                  :key="Math.random()"
                  v-hover="row.skuImg"
                  :src="row.skuImg + '?x-oss-process=image/resize,w_100/quality,q_70'"
                />
              </template>
            </el-table-column>
            <!-- 库存 -->
            <el-table-column :label="$t('库存')" prop="skuStock" align="center" />
            <!-- 采购链接 -->
            <el-table-column show-overflow-tooltip :label="$t('采购链接')" align="center">
              <template slot-scope="{ row }">
                <el-link :href="row.skuUrl" target="_blank">
                  {{ row.skuUrl }}
                </el-link>
              </template>
            </el-table-column>
            <!-- 上次采购时间 -->
            <el-table-column show-overflow-tooltip :label="$t('上次采购时间')" prop="purchaseTime" align="center" />
          </el-table>
          <pagination
            :total="manufacturersSku.pageTotal"
            :page.sync="manufacturersSku.queryParams.pageNum"
            :limit.sync="manufacturersSku.queryParams.pageSize"
            @pagination="getManufacturersSkuListByPurchase"
          />
        </div>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import {
  getManufacturersListOld,
  getManufacturerList,
  getConversionRateList,
  getPurchaseInfo,
  createPurchaseOrderCn,
  createPurchaseOrderJp,
  getManufacturers,
  getManufacturersSkuListByPurchase,
  updateManufacturersSkuUrl,
  getSkuPurchaseList,
} from '@/api/purchasingManage/manufacturerPurchaseOrder'
import { useDict } from '@/utils/useDict'

export default {
  name: 'createPurchase',
  data() {
    return {
      /**最近采购记录 */
      skuPurchaseRecord: {
        tableData: [],
        pageTotal: 0,
        queryParams: {
          manufacturersId: '',
          pageNum: 1,
          pageSize: 10,
        },
      },
      /**厂家关联sku */
      manufacturersSku: {
        tableData: [],
        pageTotal: 0,
        queryParams: {
          manufacturersId: '',
          pageNum: 1,
          pageSize: 10,
        },
      },

      /** 已采购厂家列表*/
      manufacturersListOld: [],
      title: '',
      /**采购类型 */
      purchase_type_list: [],
      showName: '',
      form: {
        type: 1,
        payPrice: null,
        skuTotalValue: null,
        payPriceUnit: 1,
        choice: 1,
        num: 1,
        skuId: '',
        skuTotalValue: 0,
        skuUnitPrice: 0,
        manufacturersId: '',
      },
      /**汇率 */
      exchangeRate: 1,
      /**汇率 */
      exchangeRateList: [],
      addForm: {
        manufacturersName: '',
        manufacturersTel: '',
        manufacturersAddress: '',
        principalName: '',
        principalTel: '',
        contact: '',
      },
      /**获取供应厂家列表  （厂家） */
      manufacturersList: [],
      showDialog: false,
      rules: {
        type: [{ required: true, message: '请输入', trigger: 'blur' }],
        skuId: [{ required: true, message: '请选择', trigger: 'blur' }],
        num: [{ required: true, message: '请输入', trigger: 'blur' }],
        payPrice: [{ required: true, message: '请输入数字类型', trigger: 'blur' }],
        skuTotalValue: [{ required: true, message: '请输入', trigger: 'blur' }],
        skuUnitPrice: [{ required: true, message: '请输入', trigger: 'blur' }],
        manufacturersId: [{ required: true, message: '请选择', trigger: 'blur' }],
      },
      addRules: {
        id: [{ required: true, message: 'ID不能为空', trigger: 'blur' }],
        manufacturersName: [{ required: true, message: '厂家名称不能为空', trigger: 'blur' }],
        // manufacturersTel: [
        //   { required: true, message: "厂家电话不能为空", trigger: "blur" },
        // ],
        // principalName: [
        //   { required: true, message: "负责人名称不能为空", trigger: "blur" },
        // ],
        // principalTel: [
        //   { required: true, message: "负责人电话不能为空", trigger: "blur" },
        // ],
        // netConcat: [
        //   { required: true, message: "负责人电话不能为空", trigger: "blur" },
        // ],
        // purchasePlatformLink: [
        //   { required: true, message: "请输入", trigger: "blur" },
        // ],
        // manufacturersAddress: [
        //   { required: true, message: "厂家地址不能为空", trigger: "blur" },
        // ],
        // deliverPositionCn: [
        //   { required: true, message: "中国发货仓库不能为空", trigger: "blur" },
        // ],
        // deliverWayCn: [
        //   { required: true, message: "中国发货方式不能为空", trigger: "blur" },
        // ],
        // isDefault: [
        //   {
        //     required: true,
        //     message: "是否默认供应厂家不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // skuPackDemand: [
        //   { required: true, message: "产品捆绑要求不能为空", trigger: "blur" },
        // ],
        // remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
      loading: false,
      /**厂家详情 */
      manufacturerDetail: {
        id: '',
        distrId: '',
        manufacturersSkuId: '',
        purchasePlatformLink: '',
        manufacturersName: '',
        manufacturersTel: '',
        manufacturersAddress: '',
        principalName: '',
        principalTel: '',
        netConcat: '',
        skuUrl: '',
      },
    }
  },
  created() {
    this.$emit('getList')
    this.getTypeList()
  },
  computed: {
    unitPrice: function () {
      return +(this.form.payPrice / this.form.num).toFixed(4)
    },
  },
  methods: {
    handlePayPriceUnit(payPriceUnit) {
      let color = 'orange'
      switch (payPriceUnit) {
        case 'JPY':
          color = 'rgba(241, 129, 0, 0.973)'
          break
        case 'RMB':
          color = 'red'
          break
        case 'USD':
          color = 'rgb(24, 144, 255)'
          break

        default:
          break
      }
      return { color }
    },
    handleUpdateManufacturersSkuUrl() {
      const data = {
        id: this.manufacturerDetail.id,
        distrId: this.manufacturerDetail.distrId,
        manufacturersSkuId: this.manufacturerDetail.manufacturersSkuId,
        skuId: this.form.skuId,
        skuUrl: this.manufacturerDetail.skuUrl,
      }
      updateManufacturersSkuUrl(data).then((res) => {
        const { code, data, msg } = res
        if (code === 200) {
          this.$message({ type: 'success', message: msg })
        }
      })
    },
    getManufacturersSkuListByPurchase() {
      const data = {
        ...this.manufacturersSku.queryParams,
        skuId: this.form.skuId,
      }
      console.log(`data ==>`, data)
      getManufacturersSkuListByPurchase(data).then((res) => {
        const { code, rows, total } = res
        if (code === 200) {
          this.manufacturersSku.tableData = rows
          this.manufacturersSku.pageTotal = total
        }
      })
    },
    /**
     * 查询厂家/已采购厂家的详情
     */
    handleManufacturerDetail(id, type) {
      this.manufacturersSku.queryParams.manufacturersId = id
      this.getManufacturersSkuListByPurchase()
      let detail
      /**
       * 已采购厂家
       */
      if (type == 'old') {
        detail = this.manufacturersListOld.filter((i) => i.id == id)[0] || {}
      } else {
        /**
         * 厂家
         */
        detail = this.manufacturersList.filter((i) => i.id == id)[0] || {}
      }

      getManufacturers({
        skuId: this.form.skuId,
        manufacturersId: detail.id,
      }).then((res) => {
        // this.manufacturerDetail.skuUrl = res.data?.skuUrl;
        Object.keys(this.manufacturerDetail).forEach((key) => (this.manufacturerDetail[key] = res.data[key]))

        if (!this.manufacturerDetail.skuUrl) {
          this.manufacturerDetail.skuUrl = ''
        } else if (!this.manufacturerDetail.skuUrl.startsWith('https://')) {
          this.manufacturerDetail.skuUrl = 'https://' + this.manufacturerDetail.skuUrl
        }
      })
    },
    resetManufacturersSku() {
      /******************厂家关联sku********************* */
      this.manufacturersSku.tableData = []
      this.manufacturersSku.pageTotal = 0
      this.manufacturersSku.queryParams = {
        manufacturersId: '',
        pageNum: 1,
        pageSize: 10,
      }
      /******************厂家关联sku end***************** */
    },
    handleRadioChange(type) {
      this.resetManufacturersSku()
      this.form.manufacturersId = ''
      Object.keys(this.manufacturerDetail).forEach((key) => (this.manufacturerDetail[key] = ''))
      // console.log(`type ==>`,type);
      // if (type == 3) {
      //   this.getManufacturersListOld();
      // }
    },
    getTypeList() {
      useDict('purchase_type').then((res) => {
        const { purchase_type } = res
        this.purchase_type_list = purchase_type
      })
    },
    setVisible(type) {
      //采购类型 1-国内采购 2-酒水采购 3-日本当地采购
      if (type == 1) {
        this.form.choice = 1
      }
    },
    setSkuUnitPrice(num) {
      if (this.form.skuUnitPrice) this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed()
    },
    setInfo(e) {
      this.form.skuTotalValue = (+this.form.payPrice * +this.exchangeRate).toFixed()
      this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed()
    },
    /**获取汇率 */
    getExchangeRate() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.5)',
      })
      getConversionRateList()
        .then((res) => {
          this.exchangeRateList = res.data
          const cnRate = res.data?.filter((i) => i.baseCode == 'CNY')
          this.exchangeRate = cnRate[0].conversionRate
          this.showDialog = true
        })
        .finally(() => {
          loading.close()
        })
    },
    /**设置汇率 */
    handleExchangeRate(id) {
      // 付款总价单位 1-RMB 2-JPY 3-USD
      if (id == 1) {
        const cnRate = this.exchangeRateList?.filter((i) => i.baseCode == 'CNY')
        this.exchangeRate = cnRate[0].conversionRate
      }
      if (id == 2) {
        this.exchangeRate = 1
      }
      if (id == 3) {
        const cnRate = this.exchangeRateList?.filter((i) => i.baseCode == 'USD')
        this.exchangeRate = cnRate[0].conversionRate
      }

      // 重新计算  总货值  单价
      this.form.skuTotalValue = (+this.form.payPrice * +this.exchangeRate).toFixed()
      this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed()
    },
    handleDetail(id) {
      console.log(` ==>`, id)
      getPurchaseInfo(id).then((res) => {
        console.log(`res ==>`, res)
      })
    },
    /**获取供应厂家列表  （厂家） */
    getManufacturers() {
      getManufacturerList().then((res) => (this.manufacturersList = res.data))
    },
    resetForm() {
      this.form = {
        type: 1,
        payPrice: null,
        skuTotalValue: null,
        payPriceUnit: 1,
        choice: 1,
        num: 1,
        skuId: '',
        skuTotalValue: 0,
        skuUnitPrice: 0,
        manufacturersId: '',
      }
    },
    resetAddForm() {
      this.addForm = {
        purchasePlatformLink: '',
        manufacturersName: '',
        manufacturersTel: '',
        manufacturersAddress: '',
        principalName: '',
        principalTel: '',
        netConcat: '',
      }
    },

    handleAdd({ title, skuId, skuNo }) {
      // this.title = title
      this.resetForm()
      this.resetAddForm()
      this.form.skuNo = skuNo
      this.form.skuId = skuId
      this.showName = 'add'
      this.getManufacturers()
      this.getExchangeRate()
      this.getSkuPurchaseList()
      this.getManufacturersListOld()
    },
    /**获取sku最近采购列表 */
    getSkuPurchaseList() {
      getSkuPurchaseList({
        ...this.skuPurchaseRecord.queryParams,
        skuId: this.form.skuId,
      }).then((res) => {
        const { rows, total } = res
        this.skuPurchaseRecord.tableData = rows
        this.skuPurchaseRecord.pageTotal = total
      })
    },
    /**已采购厂家 */
    getManufacturersListOld() {
      getManufacturersListOld(this.form.skuId).then((res) => {
        this.manufacturersListOld = res.data
      })
    },
    handleEdit(row) {
      this.showName = 'edit'
      const _row = JSON.parse(JSON.stringify(row))
      const time = _row.operateTime.split('-')
      _row.operateStartTime = time[0]
      _row.operateEndTime = time[1]
      this.form = _row
      this.showDialog = true
    },

    async handleSubmit(type, data) {
      //国内
      if (type == 1) {
        const res = await createPurchaseOrderCn(data)
        if (res.code == 200) {
          this.$message({ type: 'success', message: res.msg })

          this.showDialog = false
          this.$emit('getList')
        }
      }
      //日本
      if (type == 3) {
        const res = await createPurchaseOrderJp(data)
        if (res.code == 200) {
          this.$message({ type: 'success', message: res.msg })

          this.showDialog = false
          this.$emit('getList')
        }
      }
    },
    submitForm() {
      let self = this
      // 国内和日本
      const type = this.form.type
      const choice = this.form.choice // 1.选择所有厂家   2.新增厂家    3.选择已采购厂家
      const submit = () => {
        if (choice == 2) {
          self.$refs.addForm.validate(async (valid) => {
            if (valid) {
              data.dsManufacturersVo = self.addForm
              data.manufacturersId = null
              this.handleSubmit(type, data)
            }
          })
        } else {
          data.dsManufacturersVo = {}
          this.handleSubmit(type, data)
        }
      }
      const data = JSON.parse(JSON.stringify(self.form))
      data.conversionRate = this.exchangeRate

      if (type == 1 || type == 3) {
        this.$refs.elForm.validate((valid) => {
          if (valid) {
            // 创建采购单历史价格校验
            //---如果创建的采购单价大于或小于最近历史的价格的话就要再弹个窗提示
            //“输入价格与历史价格差距过大，是否继续？”，
            //没有历史价格不用校验
            if (this.skuPurchaseRecord.tableData.length) {
              const row = this.skuPurchaseRecord.tableData[0]
              const recentPrice = ((row.payPrice || 0) / (row.num || 0)).toFixed(2)
              if (recentPrice != this.unitPrice) {
                this.$confirm(this.$t('输入价格与历史价格差距过大,是否继续?'), this.$t('提示'), {
                  type: 'warning',
                })
                  .then(() => {
                    submit()
                  })
                  .catch((e) => e)
              } else {
                submit()
              }
            } else {
              submit()
            }
          }
        })
      }

      // 酒水
      if (type == 2) {
        return
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.my-dialog {
  display: flex;
  .left-form {
    flex: 1;
  }
  .right-table {
    // flex: 2;
    width: 600px;
    margin-top: -30px;
    margin-left: 20px;
    display: flex;
    flex-direction: column;
    &_title {
      font-weight: bold;
      margin: 5px 0;
    }
    .table {
      border: 1px solid #eee;
    }
  }
}

::v-deep .el-table .el-table__cell {
  padding: 4px !important;
}
</style>
